﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class FAQforADMIN : System.Web.UI.Page
{
    string strConnect;
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        strConnect = "Data Source = (local); Database =photofpt; User id = sa; Password = 30041975";
        con = new SqlConnection(strConnect);
        ds = new DataSet();
        if (!IsPostBack)
            BindFAQ();
    }
    protected void BindFAQ()
    {
        try
        {
            if (con.State == ConnectionState.Closed)
                con.Open();
            cmd = new SqlCommand("getFAQ", con);
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "FAQ");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();


        }
        catch (SqlException ex)
        {
            //throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected void addnewbtn_Click(object sender, EventArgs e)
    {
        try
        {
            if (con.State == ConnectionState.Open)
                con.Close();
            else
                con.Open();
            cmd = new SqlCommand("addFAQ", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Answer", SqlDbType.NVarChar).Value = txtAnswer.Text;
            cmd.Parameters.Add("@Question", SqlDbType.NVarChar).Value = txtQuestion.Text;
            
            cmd.ExecuteNonQuery();
            //upload image to server
            Response.Redirect("FAQforADMIN.aspx");

            
            
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected void Row_Editting(object sender, GridViewEditEventArgs e)
    {
       /* GridView1.EditIndex = e.NewEditIndex;
        BindFAQ();*/
    }
    protected void Row_Updating(object sender, GridViewUpdateEventArgs e)
    {
        /*ds = new DataSet();
        if (con.State == ConnectionState.Closed)
            con.Open();

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int id = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
        string question = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string answer = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        

        cmd = new SqlCommand("UpdateFAQ", con);
        cmd.CommandType = CommandType.StoredProcedure;


        cmd.Parameters.AddWithValue("@ID", id);
        cmd.Parameters.AddWithValue("@Question", question);
        cmd.Parameters.AddWithValue("@Answer", answer);
        
        //conn.Open();
        cmd.ExecuteNonQuery();

        GridView1.EditIndex = -1;
        BindFAQ();
        con.Close();

        txtQuestion.Text = "";
        txtAnswer.Text = "";*/
    }
    protected void Row_Deleting(object sender, GridViewDeletedEventArgs e)
    {
        /*try
        {
            ds = new DataSet();
            if (con.State == ConnectionState.Closed)
                con.Open();

            //GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            //int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);

            cmd = new SqlCommand("delFAQ", con);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            BindFAQ();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }*/
    }
    protected void Edit(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindFAQ();
    }
    protected void Update(object sender, GridViewUpdateEventArgs e)
    {
        ds = new DataSet();
        if (con.State == ConnectionState.Closed)
            con.Open();

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int id = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
        string question = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string answer = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;


        cmd = new SqlCommand("UpdateFAQ", con);
        cmd.CommandType = CommandType.StoredProcedure;


        cmd.Parameters.AddWithValue("@ID", id);
        cmd.Parameters.AddWithValue("@Question", question);
        cmd.Parameters.AddWithValue("@Answer", answer);

        //conn.Open();
        cmd.ExecuteNonQuery();

        GridView1.EditIndex = -1;
        BindFAQ();
        con.Close();

        txtQuestion.Text = "";
        txtAnswer.Text = "";
    }
    protected void Delete(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            ds = new DataSet();
            if (con.State == ConnectionState.Closed)
                con.Open();

            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            //int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);

            cmd = new SqlCommand("delFAQ", con);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            BindFAQ();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected void Cancel(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindFAQ();
    }
}